package com.eternal.base.database.dao;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.eternal.base.concat.DeviceInfo;
import com.eternal.base.concat.DeviceModel;
import com.eternal.base.concat.DeviceModelInfo;
import com.eternal.base.concat.DeviceName;
import com.eternal.base.concat.DeviceSetting;
import com.eternal.base.concat.PortInfo;
import com.eternal.base.database.entity.Device;

import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by Administrator
 * On 2020/4/30
 * Description:
 *
 * @author Administrator
 */
@Dao
public interface DeviceDao {
    /**
     * 获取所有设备信息
     *
     * @return
     */
    @Query("select mac,name, typeName, type, connectTime, tmp, hum, fan,isDegree, tmpState, humState,fanState,fanType, port,choosePort, isPlug from Device")
    Flowable<List<DeviceInfo>> loadAllDeviceInfo();

    /**
     * 获取设备所有端口信息
     *
     * @return
     */
    @Query("select mac,name, typeName, type, connectTime, tmp, hum, fan,isDegree, tmpState, humState,fanState,fanType, port, choosePort, isPlug from Device where mac=:mac")
    List<DeviceInfo> loadPortInfo(String mac);

    /**
     * 更新连接时间
     *
     * @param mac  mac
     * @param time 时间
     */
    @Query("update device set connectTime=:time where mac=:mac")
    void updateTime(String mac, long time);

    /**
     * 获取指定mac设备信息
     *
     * @param mac mac
     * @return 设备信息
     */
    @Query("select mac,name, typeName, type, connectTime, tmp, hum, fan,isDegree, tmpState, humState,fanState, fanType, port,choosePort, isPlug from device where mac=:mac")
    Flowable<DeviceInfo> loadDeviceInfo(String mac);

    @Query("select type from device where mac=:mac")
    byte loadDeviceType(String mac);

    /**
     * 删除设备
     *
     * @param mac mac
     */
    @Query("delete from device where mac=:mac")
    int delete(String mac);

    /**
     * 插入设备
     *
     * @param device 设备
     */
    @Insert
    void insert(Device device);

    /**
     * 获得所有设备的名字
     *
     * @return 设备名字
     */
    @Query("select mac, name from device")
    List<DeviceName> loadAllName();

    @Query("select mac from device")
    List<String> loadAllMac();


    /**
     * 获得设备设置信息
     *
     * @param mac mac
     * @return 设置信息
     */
    @Query("select name, isDegree, brightness, transitionTemperature, transitionHumidity," +
            "calibrationTemperature, calibrationHumidity from device where mac=:mac and port=:port")
    DeviceSetting loadSettings(String mac, byte port);

    /**
     * 修改设置信息
     *
     * @param mac        设备
     * @param name       名字
     * @param brightness 背光
     * @param transTmp   trans温度
     * @param transHum   trans湿度
     * @param caliTmp    cali温度
     * @param caliHum    cali湿度
     */
    @Query("update device set name=:name,brightness=:brightness, isDegree=:isDegree" +
            ",transitionTemperature=:transTmp,transitionHumidity=:transHum" +
            ",calibrationTemperature=:caliTmp, calibrationHumidity=:caliHum" +
            " where mac=:mac and port=:port")
    void updateSetting(String mac, byte port, String name, byte brightness, boolean isDegree,
                       byte transTmp, byte transHum, byte caliTmp, byte caliHum);

    /**
     * 同步设置信息
     *
     * @param mac        设备
     * @param brightness 背光
     * @param transTmp   trans温度
     * @param transHum   trans湿度
     * @param caliTmp    cali温度
     * @param caliHum    cali湿度
     */
    @Query("update device set brightness=:brightness, isDegree=:isDegree" +
            ",transitionTemperature=:transTmp,transitionHumidity=:transHum" +
            ",calibrationTemperature=:caliTmp, calibrationHumidity=:caliHum" +
            " where mac=:mac and port=:port")
    void updateSetting(String mac, byte port, byte brightness, boolean isDegree,
                       byte transTmp, byte transHum, byte caliTmp, byte caliHum);

    /**
     * 获取设备模式信息
     *
     * @param mac 设备mac
     * @return模式信息
     */
    @Query("select isControlTypeByHum, workType, typeOff, typeOn, timeOff, timeOn, cycleOff, cycleOn, " +
            "schedOff, schedOn, autoHighHum, autoHighTmpSwitch, autoLowHum, autoLowHumSwitch, autoHighTmp, " +
            "autoHighTmpSwitch, autoLowTmp, autoLowTmpSwitch, autoHighHumSwitch, currentTypeResidueOn, currentTypeResidueTime, " +
            "isDegree, fan, hum, tmp,fanState, humState,tmpState,fanType, port,choosePort from device where mac=:mac and port=:port")
    DeviceModel loadDeviceModel(String mac, byte port);

    /**
     * 获取设备模式信息
     *
     * @param mac 设备mac
     * @return模式信息
     */
    @Query("select mac, name from device where mac=:mac and port=:port")
    Flowable<DeviceName> loadDeviceName(String mac, byte port);

    /**
     * @param mac
     * @return
     */
    @Query("select isDegree, schedOff, schedOn, cycleOn, cycleOff, timeOn, timeOff, " +
            "autoHighHum, autoHighTmp, autoLowHum, autoLowTmp , autoHighTmpSwitch, autoLowTmpSwitch," +
            "autoHighHumSwitch, autoLowHumSwitch from device where mac=:mac")
    DeviceModelInfo loadModelInfo(String mac);

    /**
     * 获取设备日志时间
     *
     * @param mac 设备
     * @return 日志时间
     */
    @Query("select logTime from device where mac=:mac and port=:port")
    long loadLogTime(String mac, byte port);

    /**
     * 获取设备日志时间
     *
     * @param mac 设备
     * @return 日志时间
     */
    @Query("select logTime from device where mac=:mac")
    long loadLogTime(String mac);

    /**
     * 更新设备日志时间
     *
     * @param mac  设备mac
     * @param time 日志时间
     */
    @Query("update device set logTime=:time where mac=:mac and port=:port")
    void updateDeviceLogTime(String mac, byte port, long time);

    /**
     * 获取设备历史温湿度时间
     *
     * @param mac 设备
     * @return 时间
     */
    @Query("select historyTime from device where mac=:mac")
    long loadDeviceHistoryTime(String mac);

    /**
     * 设置设备历史温湿度时间
     *
     * @param mac  设备
     * @param time 时间
     */
    @Query("update device set historyTime=:time where mac=:mac")
    void updateDeviceHistoryTime(String mac, long time);

    @Query("update device set isDegree=:isDegree,tmp=:tmp,hum=:hum,fan=:fan,isControlTypeByHum=:isControlTypeByHum," +
            "workType=:workType,typeOn=:typeOn,typeOff=:typeOff,timeOn=:timeOn,timeOff=:timeOff,cycleOn=:cycleOn," +
            "cycleOff=:cycleOff,schedOn=:schedOn,schedOff=:schedOff,autoHighHum=:autoHighHum,autoLowHum=:autoLowHum," +
            "autoHighTmp=:autoHighTmp,autoLowTmp=:autoLowTmp,autoHighTmpSwitch=:autoHighTmpSwitch,autoLowTmpSwitch=:autoLowTmpSwitch," +
            "autoHighHumSwitch=:autoHighHumSwitch,autoLowHumSwitch=:autoLowHumSwitch,currentTypeResidueTime=:currentTypeResidueTime," +
            "currentTypeResidueOn=:currentTypeResidueOn where mac=:mac and port=:port")
    void updateDeviceModel(String mac, byte port, boolean isDegree, int tmp, int hum, int fan, boolean isControlTypeByHum,
                           byte workType, byte typeOn, byte typeOff, char timeOn, char timeOff, char cycleOn,
                           char cycleOff, char schedOn, char schedOff, byte autoHighHum, byte autoLowHum,
                           byte autoHighTmp, byte autoLowTmp, boolean autoHighTmpSwitch, boolean autoLowTmpSwitch,
                           boolean autoHighHumSwitch, boolean autoLowHumSwitch, int currentTypeResidueTime,
                           boolean currentTypeResidueOn);

    @Query("update device set timeOn=:timeOn, timeOff=:timeOff, cycleOn=:cycleOn, cycleOff=:cycleOff, schedOn=:schedOn, schedOff=:schedOff," +
            "autoHighHum=:autoHighHum, autoLowHum=:autoLowHum, autoHighTmp=:autoHighTmp, autoLowTmp=:autoLowTmp," +
            "autoHighTmpSwitch=:autoHighTmpSwitch, autoLowTmpSwitch=:autoLowTmpSwitch,autoHighHumSwitch=:autoHighHumSwitch," +
            "autoLowHumSwitch=:autoLowHumSwitch,choosePort=:choosePort where mac=:mac and port=:port")
    void updateModel(String mac, char timeOn, char timeOff, char cycleOn, char cycleOff, char schedOn,
                     char schedOff, byte autoHighHum, byte autoLowHum, byte autoHighTmp, byte autoLowTmp,
                     boolean autoHighTmpSwitch, boolean autoLowTmpSwitch, boolean autoHighHumSwitch, boolean autoLowHumSwitch, byte port, byte choosePort);

    @Query("update device set isDegree=:isDegree,tmp=:tmp,hum=:hum,fan=:fan,tmpState=:tmpState, humState=:humState, fanType=:fanType,choosePort=:choosePort where mac=:mac")
    void updateDeviceInfo(String mac, boolean isDegree, int tmp, int hum, int fan, byte tmpState, byte humState, byte choosePort, byte fanType);

    @Query("update device set fan=:fan, fanState=:fanState, fanType=:fanType, fan=:fan, isPlug=:isPlug where mac=:mac and port=:id")
    void updatePortInfo(String mac, byte id, byte fanState, int fan, byte fanType, boolean isPlug);

    @Query("select isDegree from device where mac=:mac")
    boolean isDegree(String mac);

    @Query("select connectTime from device where mac=:mac")
    long getConnectTime(String mac);

    /**
     * 更新控制页面显示类型
     *
     * @param mac                mac
     * @param isControlTypeByHum 控制页面是否是湿度  true湿度  false 温度
     */
    @Query("update device set isControlTypeByHum=:isControlTypeByHum where mac=:mac and port=:port")
    void updateControlTypeByHum(String mac, byte port, boolean isControlTypeByHum);
}
